package cn.u313.lymusic.viewmodel

import android.util.Log
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.livedata.observeAsState
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.asFlow
import androidx.lifecycle.viewModelScope
import cn.u313.lymusic.model.SearchModel
import cn.u313.lymusic.utils.OkHttpUtil
import com.alibaba.fastjson.JSON
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import java.net.URLEncoder

class MusicDownloadViewModel : ViewModel() {
    val searchModel = MutableLiveData<SearchModel?>()
    val searchMoreModel = MutableLiveData<SearchModel?>()
    val musicInfo = MutableLiveData<SearchModel.Data?>()
    val isLoadding = MutableLiveData<Boolean>()
    fun showLoading(){
        isLoadding.postValue(true)
    }
    fun hideLoading(){
        isLoadding.postValue(false)
    }
    val playUrl = MutableLiveData<String?>()
    val downloadUrl = MutableLiveData<String?>()
    fun search(name: String) {
        Log.e(">>", "search: $name" )
        viewModelScope.launch(Dispatchers.IO) {
            val str = OkHttpUtil.get(
                "https://y.313u.cn/v2/cgi_bin/music/search?name=${
                    URLEncoder.encode(
                        name,
                        "Utf-8"
                    )
                }&p=1&n=20"
            )
            Log.e(">>", "search: $str" )
            val toJavaObject = JSON.toJavaObject(JSON.parseObject(str), SearchModel::class.java)
            Log.e(">>", "search: $toJavaObject" )
            searchModel.postValue(toJavaObject)
            withContext(Dispatchers.Main){
                hideLoading()
            }
        }
    }
    fun search(name: String,p:Int ,n:Int = 20) {
        Log.e(">>", "search: $name" )
        viewModelScope.launch(Dispatchers.IO) {
            val str = OkHttpUtil.get(
                "https://y.313u.cn/v2/cgi_bin/music/search?name=${
                    URLEncoder.encode(
                        name,
                        "Utf-8"
                    )
                }&p=$p&n=$n"
            )
            Log.e(">>", "search: $str" )
            val toJavaObject = JSON.toJavaObject(JSON.parseObject(str), SearchModel::class.java)
            Log.e(">>", "search: $toJavaObject" )
            searchMoreModel.postValue(toJavaObject)
            withContext(Dispatchers.Main){
                hideLoading()
            }
        }
    }

    fun download(mid: String,q:String) {
        Log.e(">>", "search: $mid" )
        showLoading()
        viewModelScope.launch(Dispatchers.IO) {
            val str = OkHttpUtil.get(
                "https://y.313u.cn/v2/cgi_bin/music/play?mid=${mid.replace("MUSIC_","")}&quality="+q
            )
            Log.e(">>", "search: $str" )
            downloadUrl.postValue(JSON.parseObject(str).getString("data"))
            hideLoading()
        }
    }
    fun play(mid: String,q:String) {
        Log.e(">>", "search: $mid" )
        showLoading()
        viewModelScope.launch(Dispatchers.IO) {
            val str = OkHttpUtil.get(
                "https://y.313u.cn/v2/cgi_bin/music/play?mid=${mid.replace("MUSIC_","")}&quality="+q
            )
            Log.e(">>", "search: $str" )
            playUrl.postValue(JSON.parseObject(str).getString("data"))
            hideLoading()
        }
    }
}